package com.base.syslib.util;

import java.util.Stack;

public class SequenceUtil {

    private Stack sequenceUtilPool = null;
    private static SequenceUtil instance = null;

    private SequenceUtil() {
        sequenceUtilPool = new Stack();
    }

    /**
     * 返回长度为20位的随机字符串
     *
     * @return
     */

    public String getRandomID() {
        SequenceId sequenceId = getSequenceId();
        String sequence = sequenceId.getSequence();
        releaseUtil(sequenceId);
        return sequence;
    }

    public static synchronized SequenceUtil getInstance() {
        if (instance == null) {
            instance = new SequenceUtil();
        }
        return instance;
    }

    private SequenceId getSequenceId() {
        SequenceId sequenctId = null;
        if (sequenceUtilPool.size() > 0)
            sequenctId = (SequenceId) sequenceUtilPool.pop();
        else
            sequenctId = new SequenceId();

        return sequenctId;
    }

    private void releaseUtil(SequenceId sequenctId) {
        sequenceUtilPool.push(sequenctId);

    }

}
